package common ;

import ConnDatabase.* ;
import java.util.* ;
import java.sql.* ;
import javax.servlet.jsp.* ;
import javax.servlet.jsp.tagext.* ;
public class CompanyListTag extends TagSupport {
	
	private String projectId ;
	Collection companys ;
	Collection persons ;
	String projectIdStr ;
	
	public CompanyListTag() {
		
	}
	
	public void setProjectId(String setValue) {
		projectId = setValue.trim() ;
		if (projectId.equals("")) 
			projectIdStr = "" ;
		else 
			projectIdStr = " and projectid=" + projectId ;
				
	}	
	
	public int doStartTag() throws JspTagException {
		companys = getCompanys() ;
		persons = getPersons() ;
		return EVAL_BODY_INCLUDE ;			
	}
	
	public int doEndTag() {
		companys = null ;
		persons = null ;
		return EVAL_PAGE ;
	}		
	
	public List getCompanys() {
		String companyStr = "select id,projectid,companytype,companyname," +
				"companyaddress,companytelephone from jl_projectcompany" +
				" where id>0 " + projectIdStr;
		ArrayList arrayList = new ArrayList() ;
		try {
			ConnDatabase conn2 = new ConnDatabase() ;
			ResultSet rs = conn2.executeQuery(companyStr) ;
			int id = 0 ;
			int projectId = 0 ;
			String companyType = "" ;
			String companyName = "" ;
			String companyAddress = "" ;
			String companyTelephone = "" ;
			
			
			while (rs.next()) {
				id = rs.getInt("id") ;
				projectId = rs.getInt("projectid") ;
				companyType = rs.getString("companytype") ;
				companyName = rs.getString("companyName") ;
				companyAddress = rs.getString("companyaddress") ;
				companyTelephone = rs.getString("companytelephone") ;
				
				ProjectCompany projectCompany = new ProjectCompany(id,
								projectId,companyType,companyName,
								companyAddress,companyTelephone) ;
				arrayList.add(projectCompany) ;
				
			}	
		} catch (Exception e) {
			System.out.println("companylisttag ++ " + e) ;
		}		
		return arrayList ;		
	}
	
	public List getPersons() {
		String personStr = "select id,projectid,companytype,companyid," +
				"personname,personposition,persontrust,persontelephone " +
				"from jl_companyperson where id>0  " + projectIdStr ;
		int id = 0 ;
		int projectId = 0 ;
		String companyType = "" ;
		int companyId = 0 ;
		String personName = "" ;
		String personPosition = "" ;
		String personTrust = "" ;
		String personTelephone = "" ;
		ArrayList arrayList = new ArrayList() ;
		try {
			ConnDatabase  conn3 = new ConnDatabase() ;
			ResultSet rs = conn3.executeQuery(personStr) ;
			
			while (rs.next()) {
				id = rs.getInt("id") ;
				projectId = rs.getInt("projectId") ;
				companyType = rs.getString("companytype") ;
				companyId = rs.getInt("companyId") ;
				personName = rs.getString("personName") ;
				personPosition = rs.getString("personposition") ;
				personTrust = rs.getString("persontrust") ;
				personTelephone = rs.getString("personTelephone") ;
				
				CompanyPerson companyPerson = new CompanyPerson(id,projectId,
								companyType,companyId,personName,
							personPosition,personTrust,
							personTelephone) ;
				arrayList.add(companyPerson) ;					
			}
		} catch (Exception e) {
			System.out.println("companylisttag-getpersons+++ " + e) ;
		}		
		return arrayList ;		
	}	
	
	
	
	


}
